package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

import retrieval.QueryFrame;
import ui.classification.ClaVisualFrame;

/**
 * main entrance of the whole project
 * 
 * @author huangcd
 * 
 */
public class Main extends JFrame
{
    private static Dimension   screenSize;
    private static Dimension   defaultSize;
    private static Point       location;
    private StreamControlFrame streamController;
    private CluVisualFrame     cluVisual;
    private QueryFrame         queryFrame;
    private ClaVisualFrame     classVisual;

    private JDesktopPane       desktop;

    static
    {
        defaultSize = new Dimension(950, 750);
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - defaultSize.width) / 2;
        int y = (screenSize.height - defaultSize.height) / 2;
        location = new Point(x, y);
    }

    public Main()
    {
        super("XML流数据处理平台");
        init();
        try
        {
            com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.97f);
        }
        catch (Exception e)
        {}
    }

    private void init()
    {
        // setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(defaultSize);
        setLocation(location);
        UtilGUI.maximize(this);
        setLayout(new BorderLayout());
        add(getDesktop());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                int result = JOptionPane.showConfirmDialog(null, "是否要退出平台？");
                if (result == JOptionPane.YES_OPTION)
                {
                    System.exit(0);
                }
            }
        });
        // setResizable(false);
        setVisible(true);
    }

    private JDesktopPane getDesktop()
    {
        if (desktop == null)
        {
            desktop = new JDesktopPane();
            desktop.setLayout(null);
            desktop.add(getStreamController());
            desktop.add(getCluVisual());
            desktop.add(getQueryFrame());
            desktop.add(getClassVisual());
        }
        return desktop;
    }

    /**
     * @return the streamController
     */
    private StreamControlFrame getStreamController()
    {
        if (streamController == null)
        {
            streamController = new StreamControlFrame();
        }
        return streamController;
    }

    /**
     * @return the cluVisual
     */
    private CluVisualFrame getCluVisual()
    {
        if (cluVisual == null)
        {
            cluVisual = new CluVisualFrame();
            cluVisual.setBounds(200, 10, 700, 700);
        }
        return cluVisual;
    }

    private QueryFrame getQueryFrame()
    {
        if (queryFrame == null)
        {
            queryFrame = new QueryFrame();
            queryFrame.setBounds(40, 60, 744, 449);
        }
        return queryFrame;
    }

    private ClaVisualFrame getClassVisual()
    {
        if (this.classVisual == null)
        {
            this.classVisual = new ClaVisualFrame();
            this.classVisual.setBounds(300, 20, 800, 600);
        }
        return this.classVisual;
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(new NimbusLookAndFeel());
                    new Main();
                }
                catch (Exception e2)
                {}
            }
        });
    }
}
